<template>
  <div class="dmodel-container">
    <div class="dmodel-top">
      <div class="dmodel-search"></div>
      <div class="dmodel-btn">
        <el-button type="primary" size="small" @click="isShowDialog = true">新增</el-button>
      </div>
    </div>
    <el-scrollbar style="height: calc(100vh - 200px)" class="scrollbar" id="scroll-bar">
      <el-table :data="list">
        <el-table-column label="id" prop="data_id"></el-table-column>
        <el-table-column label="展厅名称" prop="name"></el-table-column>
        <el-table-column label="缩略图">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.title_page"
              style="width: 100px; height: 40px"
              :src="scope.row.title_page"
              :preview-src-list="[scope.row.title_page]"
              fit="cover"
            ></el-image>
            <span v-else>暂无图片</span>
          </template>
        </el-table-column>
        <el-table-column label="初始朝向角度" prop="angle"></el-table-column>
        <el-table-column label="初始坐标">
          <template slot-scope="scope">
            <span>{{ scope.row.x + ' , ' + scope.row.y + ' , ' + scope.row.z }}</span>
          </template>
        </el-table-column>
        <el-table-column label="缩放比例" prop="scale"></el-table-column>
        <el-table-column label="最后修改时间" width="160" prop="data_update_time"></el-table-column>
        <el-table-column label="模型地址" prop="modle_path"></el-table-column>

        <el-table-column label="模型" width="200">
          <template slot-scope="scope">
            <!-- multiple -->
            <el-upload
              action="/ws"
              class="avatar-uploader"
              :show-file-list="false"
              :before-upload="
                (file) => {
                  beforeModelUpload(file, scope.row.data_id);
                }
              "
            >
              <el-button size="mini">选择模型</el-button>
            </el-upload>
            <el-button type="primary" @click="qwer" :disabled="modelId !== scope.row.data_id" size="mini">
              上传模型
            </el-button>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-upload
              action=""
              class="avatar-uploader"
              :show-file-list="false"
              :before-upload="
                (file) => {
                  beforeAvatarUpload(file, scope.row.data_id);
                }
              "
            >
              <el-button size="mini">上传</el-button>
            </el-upload>
            <el-button type="primary" @click="goShowRoom(scope.row.data_id, scope.row)" size="mini">调试</el-button>
            <el-button type="primary" @click="upd3dmodle(scope.row.data_id, scope.row)" size="mini">修改</el-button>
            <el-button type="danger" @click="del3dmodle(scope.row.data_id)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-scrollbar>

    <paging ref="page" :func="getList"></paging>
    <el-dialog @closed="onClose" title="提示" :visible.sync="isShowDialog">
      <el-form :model="params" :rules="rules" ref="dataParams" label-width="120px" class="demo-dataParams">
        <el-form-item label="展厅名称" prop="name">
          <el-input v-model="params.name"></el-input>
        </el-form-item>

        <el-form-item label="初始朝向角度" prop="angle">
          <el-input v-model="params.angle" type="number"></el-input>
        </el-form-item>

        <el-form-item label="初始x坐标" prop="x">
          <el-input v-model="params.x" type="number"></el-input>
        </el-form-item>

        <el-form-item label="初始y坐标" prop="y">
          <el-input v-model="params.y" type="number"></el-input>
        </el-form-item>

        <el-form-item label="初始z坐标" prop="z">
          <el-input v-model="params.z" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button type="primary" @click="dialogConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import paging from '@/components/paging';
  import dmodelManage from '@/api/3Dmodel';
  import { getImageUrl } from '@/utils/image';
  import { qiniuUploadFile } from '@/utils/QiniuUpload';
  export default {
    name: 'dmodel-view',
    components: {
      paging,
    },
    data() {
      return {
        list: [],
        isShowDialog: false,
        updId: '',
        params: {
          name: '',
          angle: 0,
          x: 0,
          y: 0,
          z: 0,
          scale: 0,
          content: null,
        },
        modelId: '',
        model: [],
        rules: {
          name: [{ required: true, message: '请输入展厅名', trigger: 'blur' }],
        },
      };
    },
    async mounted() {
      this.refresh();
    },
    methods: {
      getImageUrl,
      goShowRoom(id) {
        this.$router.push({ path: '/showRoom', query: { data_id: id, type: '3D' } });
      },
      upd3dmodle(data_id, row) {
        this.updId = data_id;
        this.isShowDialog = true;
        this.params = row;
      },
      async del3dmodle(data_id) {
        this.$alert('是否确定删除3dmodle', '删除', {
          confirmButtonText: '确定',
          callback: async (action) => {
            if (action === 'confirm') {
              await dmodelManage.del3dmodle(data_id);
              this.refresh();
            }
          },
        });
      },
      refresh() {
        this.$refs.page.refresh();
      },
      getList(callback) {
        dmodelManage.get3dmodle().then((res) => {
          let total = res.total_count;
          this.list = res.item_list;
          callback(this.list, total);
        });
      },
      async dialogConfirm() {
        this.$refs.dataParams.validate(async (valid) => {
          if (valid) {
            this.params.angle = this.params.angle ? Number(this.params.angle) : 0;
            this.params.scale = this.params.scale ? Number(this.params.scale) : 0;
            this.params.x = this.params.x ? Number(this.params.x) : 0;
            this.params.y = this.params.y ? Number(this.params.y) : 0;
            this.params.z = this.params.z ? Number(this.params.z) : 0;
            /// ```
            // this.params.content = this.params.content ? this.params.content : JSON.stringify({});
            let res = null;
            if (this.updId) {
              res = await dmodelManage.upd3dmodle(this.updId, this.params);
            } else {
              res = await dmodelManage.set3dmodle(this.params);
            }
            if (res) {
              this.isShowDialog = false;
              this.updId = '';
              this.refresh();
            }
          }
        });
      },
      onClose() {
        this.updId = '';
        this.params = {
          name: '',
          angle: 0,
          x: 0,
          y: 0,
          z: 0,
          scale: 0,
          content: null,
        };
      },
      async beforeAvatarUpload(file, id) {
        let res = await qiniuUploadFile(file, '3dmodel');
        if (res) {
          await dmodelManage.upd3dmodle(id, { title_page: res.key });
          this.refresh();
        }

        // const formData = new FormData();
        // formData.append('上传的key值', file);
        // await dmodelManage.set3dmodleImage(id, formData);
        // this.refresh();
      },
      async beforeModelUpload(file, id) {
        this.model = [file];
        this.modelId = id;
        return false;
      },
      async qwer() {
        let res = await qiniuUploadFile(this.model[0], '3dmodel');
        if (res) {
          await dmodelManage.upd3dmodle(this.modelId, { modle_path: res.key });
          this.$message.success('上传成功');
        }

        // const formData = new FormData();
        // this.model.forEach((item) => {
        //   formData.append(item.name, item);
        //   console.log(formData);
        // });
        // await dmodelManage.setUpload3dModel(this.modelId, formData);
        // this.$message.success('上传成功');
        this.modelId = '';
        this.model = [];
        this.refresh();
      },
      test() {
        let arr = [2, 3, 2, 4]; //每场选项所选个数
        let count = 2;
        let mult = 2; //倍数
        arr.forEach((item) => {
          if (item > 1) {
            count = count + (item - 1) * count;
          }
        });
        count = mult * count;
      },
    },
  };
</script>

<style lang="less" scoped>
  .dmodel-container {
    ::v-deep {
      .cell {
        display: flex;
        div {
          margin-right: 6px;
        }
      }
    }
    height: 100%;
    .avatar-uploader {
      .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .dmodel-top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 12px;
      .dmodel-search {
        display: flex;
      }
    }
    .dmodel-list {
      display: flex;
      flex-flow: row wrap;
      width: 100%;
      height: 100%;
      .dmodel-item {
        width: calc(50% - 6px);
        height: 100px;
        display: flex;
        margin-bottom: 6px;
        border: 1px solid #f3f3f3;
        .item-content {
          margin: 10px;
          p {
            line-height: 40px;
          }
        }
      }
    }
  }
</style>
